//CustomerJavaScript

$(function () {

    //页面加载完成事件
    $.MvcSheet.Loaded = function () {




        //当前流程节点
        var nowActivityCode = $.MvcSheetUI.SheetInfo.ActivityCode;

        //当前流程id
        var nowInstanceId = $.MvcSheetUI.SheetInfo.InstanceId;

        //当前流程唯一编码
        var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");


        //商务退货?
        var businessReturnElem = $("#ctl167060");

        businessReturnElem.change(function () {
            var businessReturnVal = $.MvcSheetUI.GetControlValue("BusinessReturn01");
            if (businessReturnVal == "是" && $.MvcSheetUI.SheetInfo.IsOriginateMode) {
                $(".ServiceBackHide").fadeOut();
                $("#control105").fadeIn();
                $("#div912168").fadeIn();

                //
                $("#div967044").fadeIn();
                $("#div477348").fadeIn();

                $("#div924771").fadeIn();
                $("#div538110").fadeIn();

            } else {
                $("#control105").fadeOut();
                $("#div912168").fadeOut();
                $(".ServiceBackHide").fadeIn();
                //
                $("#div967044").fadeOut();
                $("#div477348").fadeOut();

                $("#div924771").fadeOut();
                $("#div538110").fadeOut();
            }
        })


        //发起时间
        var nowDate = new Date();
        var nowTimeStr = nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes() + ':' + nowDate.getSeconds();
        $.MvcSheetUI.SetControlValue("ComplainDate02", nowTimeStr);

        var businessReturnVal = $.MvcSheetUI.GetControlValue("BusinessReturn01");

        if (businessReturnVal == "是") {
            $(".ServiceBackHide").fadeOut();
            $("#control105").fadeIn();
        } else {
            $("#control105").fadeOut();
            $(".ServiceBackHide").fadeIn();
        }

        //QA维护节点隐藏提交按钮
        if(nowActivityCode=="Activity21"){
            $("a[title='即同意，流程将继续流转']").hide();
        }


        var workItemType=$.MvcSheetUI.SheetInfo.WorkItemType;

        if("Unspecified"==workItemType){



            $("#divTopBars").append("<li title=\"关闭\" data-action=\"Close\">" +
                "<a title=\"补偿流程\" href=\"javascript:createCompensateForm();\">" +
                "<i title=\"补偿流程\" class=\"panel-title-icon fa fa-circle-o toolImage\"></i>" +
                "<span title=\"补偿流程\" class=\"toolText\">补偿流程</span>" +
                "</a></li>");
        }


        //审批节点禁止编辑
        if(nowActivityCode=="Activity31" || nowActivityCode=="Activity32"){
            $("#Control103").find("input,select").attr("disabled",true);
            //当前登陆人
            var nowLoginUser=$.MvcSheetUI.SheetInfo.UserName;

            $("td[data-title='"+nowLoginUser+"']").parent().find("input,select").attr("disabled",false);

        }



    }

    //页面初始化之前
    $.MvcSheet.PreInit = function () {
        //当前流程节点
        var nowActivityCode = $.MvcSheetUI.SheetInfo.ActivityCode;

        //商务退货
        var businessReturnVal = $.MvcSheetUI.GetControlValue("BusinessReturn01");
        console.log("页面初始化前:"+nowActivityCode);
        if(businessReturnVal!="" && null!=businessReturnVal && null!=nowActivityCode){

            if(businessReturnVal=="是"){
                $.MvcSheet.AddAction({
                    Action: "退货流程",       // 按钮名称
                    Icon: "fa-print",           // 按钮图标
                    Text: "退货流程",           // 按钮名称
                    Color: "red",
                    OnAction: function () {
                        //当前流程id
                        var nowInstanceId = $.MvcSheetUI.SheetInfo.InstanceId;
                        //当前登陆人
                        var nowLoginUser = $.MvcSheetUI.SheetInfo.UserID;
                        //表单发起人
                        var nowFormCreatePerson = $.MvcSheetUI.SheetInfo.Originator;

                        //当前流程唯一编码
                        var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");

                        if (null == nowProjectId || "" == nowProjectId) {
                            swal("未提交", "请填写基本信息并提交", "error");
                            return false;
                        }
                        var departments=new Array("营业部","流程与IT部","质量部");

                        //当前用户是否有权限发起
                        $.ajax({
                            url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/isHaveAuth",
                            data:{
                                userId:nowLoginUser,
                                departments:departments
                            },
                            traditional:true,
                            type: 'GET',
                            dataType: 'JSON',
                            async: false,
                            success: function (data) {
                                console.log(data);
                                if (data.state == "success") {
                                    //
                                    swal("确定发起[退货流程]吗?", {
                                        buttons: {
                                            cancel: "取消",
                                            confim: "确认",
                                        },
                                    }).then((value) => {
                                            switch (value) {
                                                case "confim":
                                                    $.ajax({
                                                        url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/createGoodsReturnForm?instanceId=" + nowInstanceId,
                                                        type: 'GET',
                                                        dataType: 'JSON',
                                                        async: false,
                                                        success: function (data) {
                                                            console.log(data);
                                                            if (data.state == "success") {
                                                                //location.href=$.MvcSheetUI.PortalRoot+data.url;
                                                                window.open($.MvcSheetUI.PortalRoot + data.url, "_blank")
                                                            } else {
                                                                swal("异常",data.erroStr, "error");
                                                                return false;
                                                            }
                                                        }
                                                    })
                                            }
                                        }
                                    )
                                } else {
                                    swal("无权限", "你没有权限发起流程.", "error");
                                    return false;
                                }
                            }
                        })

                    },
                    OnActionDone: function (e) {
                        // 事件执行完成
                    },
                    PostSheetInfo: false         // 是否提交表单数据，如果 false，那么不返回表单的数据
                });
                $.MvcSheet.AddAction({
                    Action: "加急单流程",       // 按钮名称
                    Icon: "fa-print",           // 按钮图标
                    Text: "加急单流程",           // 按钮名称
                    Color: "red",
                    OnAction: function () {
                        //当前流程id
                        var nowInstanceId = $.MvcSheetUI.SheetInfo.InstanceId;
                        //当前登陆人
                        var nowLoginUser = $.MvcSheetUI.SheetInfo.UserID;
                        //表单发起人
                        var nowFormCreatePerson = $.MvcSheetUI.SheetInfo.Originator;

                        //当前流程唯一编码
                        var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");

                        if (null == nowProjectId || "" == nowProjectId) {
                            swal("未提交", "请填写基本信息并提交", "error");
                            return false;
                        }
                        var departments=new Array("营业部","流程与IT部","质量部");

                        //当前用户是否有权限发起
                        $.ajax({
                            url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/isHaveAuth",
                            data:{
                                userId:nowLoginUser,
                                departments:departments
                            },
                            traditional:true,
                            type: 'GET',
                            dataType: 'JSON',
                            async: false,
                            success: function (data) {
                                console.log(data);
                                if (data.state == "success") {
                                    //
                                    swal("确定发起[加急单流程]吗?", {
                                        buttons: {
                                            cancel: "取消",
                                            confim: "确认",
                                        },
                                    }).then((value) => {
                                            switch (value) {
                                                case "confim":
                                                    $.ajax({
                                                        url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/createUrgentForm?instanceId=" + nowInstanceId,
                                                        type: 'GET',
                                                        dataType: 'JSON',
                                                        async: false,
                                                        success: function (data) {
                                                            console.log(data);
                                                            if (data.state == "success") {
                                                                window.open($.MvcSheetUI.PortalRoot + data.url, "_blank")
                                                            } else {
                                                                swal("异常",data.erroStr, "error");
                                                                return false;
                                                            }
                                                        }
                                                    })
                                            }
                                        }
                                    )
                                } else {
                                    swal("无权限", "你没有权限发起流程.", "error");
                                    return false;
                                }
                            }
                        })

                    },
                    OnActionDone: function (e) {
                        // 事件执行完成
                    },
                    PostSheetInfo: false         // 是否提交表单数据，如果 false，那么不返回表单的数据
                });
            }else{
                $.MvcSheet.AddAction({
                    Action: "退货流程",       // 按钮名称
                    Icon: "fa-print",           // 按钮图标
                    Text: "退货流程",           // 按钮名称
                    Color: "red",
                    OnAction: function () {
                        //当前流程id
                        var nowInstanceId = $.MvcSheetUI.SheetInfo.InstanceId;
                        //当前登陆人
                        var nowLoginUser = $.MvcSheetUI.SheetInfo.UserID;
                        //表单发起人
                        var nowFormCreatePerson = $.MvcSheetUI.SheetInfo.Originator;

                        //当前流程唯一编码
                        var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");

                        if (null == nowProjectId || "" == nowProjectId) {
                            swal("未提交", "请填写基本信息并提交", "error");
                            return false;
                        }
                        var departments=new Array("营业部","流程与IT部","质量部");

                        //当前用户是否有权限发起
                        $.ajax({
                            url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/isHaveAuth",
                            data:{
                                userId:nowLoginUser,
                                departments:departments
                            },
                            traditional:true,
                            type: 'GET',
                            dataType: 'JSON',
                            async: false,
                            success: function (data) {
                                console.log(data);
                                if (data.state == "success") {
                                    //
                                    swal("确定发起[退货流程]吗?", {
                                        buttons: {
                                            cancel: "取消",
                                            confim: "确认",
                                        },
                                    }).then((value) => {
                                            switch (value) {
                                                case "confim":
                                                    $.ajax({
                                                        url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/createGoodsReturnForm?instanceId=" + nowInstanceId,
                                                        type: 'GET',
                                                        dataType: 'JSON',
                                                        async: false,
                                                        success: function (data) {
                                                            console.log(data);
                                                            if (data.state == "success") {
                                                                //location.href=$.MvcSheetUI.PortalRoot+data.url;
                                                                window.open($.MvcSheetUI.PortalRoot + data.url, "_blank")
                                                            } else {
                                                                swal("异常",data.erroStr, "error");
                                                                return false;
                                                            }
                                                        }
                                                    })
                                            }
                                        }
                                    )
                                } else {
                                    swal("无权限", "你没有权限发起流程.", "error");
                                    return false;
                                }
                            }
                        })

                    },
                    OnActionDone: function (e) {
                        // 事件执行完成
                    },
                    PostSheetInfo: false         // 是否提交表单数据，如果 false，那么不返回表单的数据
                });
                $.MvcSheet.AddAction({
                    Action: "加急单流程",       // 按钮名称
                    Icon: "fa-print",           // 按钮图标
                    Text: "加急单流程",           // 按钮名称
                    Color: "red",
                    OnAction: function () {
                        //当前流程id
                        var nowInstanceId = $.MvcSheetUI.SheetInfo.InstanceId;
                        //当前登陆人
                        var nowLoginUser = $.MvcSheetUI.SheetInfo.UserID;
                        //表单发起人
                        var nowFormCreatePerson = $.MvcSheetUI.SheetInfo.Originator;

                        //当前流程唯一编码
                        var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");

                        if (null == nowProjectId || "" == nowProjectId) {
                            swal("未提交", "请填写基本信息并提交", "error");
                            return false;
                        }
                        var departments=new Array("营业部","流程与IT部","质量部");

                        //当前用户是否有权限发起
                        $.ajax({
                            url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/isHaveAuth",
                            data:{
                                userId:nowLoginUser,
                                departments:departments
                            },
                            traditional:true,
                            type: 'GET',
                            dataType: 'JSON',
                            async: false,
                            success: function (data) {
                                console.log(data);
                                if (data.state == "success") {
                                    //
                                    swal("确定发起[加急单流程]吗?", {
                                        buttons: {
                                            cancel: "取消",
                                            confim: "确认",
                                        },
                                    }).then((value) => {
                                            switch (value) {
                                                case "confim":
                                                    $.ajax({
                                                        url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/createUrgentForm?instanceId=" + nowInstanceId,
                                                        type: 'GET',
                                                        dataType: 'JSON',
                                                        async: false,
                                                        success: function (data) {
                                                            console.log(data);
                                                            if (data.state == "success") {
                                                                window.open($.MvcSheetUI.PortalRoot + data.url, "_blank")
                                                            } else {
                                                                swal("异常",data.erroStr, "error");
                                                                return false;
                                                            }
                                                        }
                                                    })
                                            }
                                        }
                                    )
                                } else {
                                    swal("无权限", "你没有权限发起流程.", "error");
                                    return false;
                                }
                            }
                        })

                    },
                    OnActionDone: function (e) {
                        // 事件执行完成
                    },
                    PostSheetInfo: false         // 是否提交表单数据，如果 false，那么不返回表单的数据
                });
            }

        }

    }

    //提交前请求唯一编号
    $.MvcSheet.Validate = function () {
        //当前流程节点
        var nowActivityCode = $.MvcSheetUI.SheetInfo.ActivityCode;


        //是否商务退货
        var BusinessState=$.MvcSheetUI.GetControlValue("BusinessReturn01");

        if("是"==BusinessState && nowActivityCode == "Activity8"){
            //商务退货评审人放入
            var appRovalElem=$.MvcSheetUI.GetElement("ReturnApproval09").SheetGridView();
            var createOUManagerCode=null;

            //获取当前发起人部门经理
            var createOUCode=$.MvcSheetUI.SheetInfo.OriginatorOU;
            var inputParam={"ServiceCode":"StaticServiceMethod","RunMethod":"findOUManagerByCode","InParams":[{"LogicTypeString":"ShortString","LogicType":14,"ItemName":"ObjectID","ItemValue":createOUCode}]};

            $.ajax({
                type: "POST",
                url: $.MvcSheetUI.PortalRoot + "/BizService/GetMethodReturnList",
                data: {formData: JSON.stringify(inputParam)},
                dataType: 'JSON',
                async: false,
                success: function (data) {
                    createOUManagerCode=data.Extend[0].ItemValue[1][0];
                }
            })

            //先删除
            if (!IsMobile) {
                appRovalElem.clearbtn.click();
            } else {
                $("div[data-datafield='ReturnApproval09']").find(".delete").click();
            }

            if(IsMobile){
                appRovalElem.addbtnBottom.click();
                appRovalElem.addbtnBottom.click();
            }else{
                appRovalElem.addbtn.click();
                appRovalElem.addbtn.click();
            }

            $.MvcSheetUI.SetControlValue("ReturnApproval09.Role09","部门经理",1);
            $.MvcSheetUI.SetControlValue("ReturnApproval09.Name09",createOUManagerCode,1);

            $.MvcSheetUI.SetControlValue("ReturnApproval09.Role09","总经理",2);
            $.MvcSheetUI.SetControlValue("ReturnApproval09.Name09","e115c519-8395-4da2-b507-8a67ab3e0213",2);

        }

        //请求项目编号
        if (this.Action == "Submit") {
            if (nowActivityCode == "Activity8") {
                //获取唯一编码
                $.ajax({
                    url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/getNewProjectNumber",
                    type: 'GET',
                    dataType: 'JSON',
                    async: false,
                    success: function (data) {
                        if (data.code == 200) {
                            console.log("请求到项目编号:" + data.data);
                            $.MvcSheetUI.SetControlValue("Number02", data.data);
                        }
                    }
                })
            }
        }


        //保存后从代办中移除
        if(this.Action=="Save" && $.MvcSheetUI.SheetInfo.ActivityCode=="Activity21"){

            var nowUser=$.MvcSheetUI.SheetInfo.UserName;
            var nowWorkItemId=$.MvcSheetUI.SheetInfo.WorkItemId;

            var param = {};
            param.ServiceCode = "CustomerProcessService";//业务服务编码
            param.RunMethod = "delProcessbyName";//方法编码

            var data = [];

            var userNameObj = {};
            userNameObj.LogicType = 14;
            userNameObj.ItemValue = nowUser;
            userNameObj.ItemName = "ParticipantName";
            userNameObj.LogicTypeString = "ShortString";
            data.push(userNameObj);

            var objectId = {};
            objectId.LogicType = 14;
            objectId.ItemValue = nowWorkItemId;
            objectId.ItemName = "ObjectID";
            objectId.LogicTypeString = "ShortString";
            data.push(objectId);

            param.InParams = data;

            $.ajax({
                type: "POST",
                url: $.MvcSheetUI.PortalRoot + "/BizService/GetMethodReturnList",
                data: {formData: JSON.stringify(param)},
                dataType: 'JSON',
                async: false,
                success: function (data) {
                    console.log(data);
                }
            });

            return true;

        }


    }




})

function createBackGoodsMethod() {
    var nowActivityCode = $.MvcSheetUI.SheetInfo.ActivityCode;

    if(nowActivityCode=="Activity8"){
        swal("提示", "请先填写基本信息并提交!", "error");
        return false;
    }
    $("a[title='退货流程']").click();
}

function createCompensateMethod() {
    var nowActivityCode = $.MvcSheetUI.SheetInfo.ActivityCode;
    swal("提示", "请先填写基本信息并提交!", "error");
    return false;
    $("a[title='索赔流程']").click();
}

function createCompensateForm() {
    //当前流程id
    var bizObjectId = $.MvcSheetUI.SheetInfo.BizObjectID;

    var instanceId="";

    $.ajax({
        url:$.MvcSheetUI.PortalRoot +'/BizService/GetMethodReturnList',
        type:'POST',
        async:false,
        data:{formData:"{\"ServiceCode\":\"CustomerProcessService\",\"RunMethod\":\"findInstanceIdByObjectId\"," +
                "\"InParams\":[{\"LogicTypeString\":\"ShortString\",\"LogicType\":14,\"ItemName\":\"BizObjectId\"," +
                "\"ItemValue\":\""+bizObjectId+"\"}]}"},
        dataType:'JSON',
        success:function (data) {
            instanceId=data.Extend[0].ItemValue[1];
        }
    })

    if(null==instanceId || ""==instanceId){
        swal("System Erro", "数据异常请联系管理员", "error");
        return false;
    }


    //当前登陆人
    var nowLoginUser = $.MvcSheetUI.SheetInfo.UserID;

    //表单发起人
    var nowFormCreatePerson = $.MvcSheetUI.SheetInfo.Originator;

    //当前流程唯一编码
    var nowProjectId = $.MvcSheetUI.GetControlValue("Number02");

    if (null == nowProjectId || "" == nowProjectId) {
        swal("未提交", "请填写基本信息并提交", "error");
        return false;
    }

    var departments=new Array("营业部","流程与IT部","质量部");

    //当前用户是否有权限发起
    $.ajax({
        url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/isHaveAuth",
        data:{
            userId:nowLoginUser,
            departments:departments
        },
        traditional:true,
        type: 'GET',
        dataType: 'JSON',
        async: false,
        success: function (data) {
            console.log(data);
            if (data.state == "success") {
                //
                swal("确定发起[索赔流程]吗?", {
                    buttons: {
                        cancel: "取消",
                        confim: "确认",
                    },
                }).then((value) => {
                    switch (value) {

                        case
                        "confim"
                        :
                            $.ajax({
                                url: $.MvcSheetUI.PortalRoot + "/CustomerComplaintsController/createCompansateForm?instanceId=" + instanceId,
                                type: 'GET',
                                dataType: 'JSON',
                                async: false,
                                success: function (data) {
                                    console.log(data);
                                    if (data.state == "success") {
                                        //location.href=$.MvcSheetUI.PortalRoot+data.url;
                                        window.open($.MvcSheetUI.PortalRoot + data.url, "_blank")
                                    } else {
                                        swal("异常",data.erroStr, "error");
                                        return false;
                                    }
                                }
                            })
                    }
                });
            } else {
                swal("无权限", "你没有权限发起流程.", "error");
                return false;
            }
        }
    })
}
